<?php
include_once './inc/config_inc.php';
include_once './inc/mysql_inc.php';
include_once './inc/tool_inc.php';
$link=connect();
$user_id=is_login($link);
if($user_id){
	skip('index.php','error','你已经登录，请不要重复登录！');exit();
}
if(isset($_POST['submit'])){
	include 'inc/check_login_inc.php';
	escape($link,$_POST);
	$query="select * from user where name='{$_POST['name']}' and pw=md5('{$_POST['pw']}')";
	$result=execute($link, $query);
	if(mysqli_num_rows($result)==1){
		setcookie('blog[name]',$_POST['name'],time()+$_POST['time']);
		setcookie('blog[pw]',sha1(md5($_POST['pw'])),time()+$_POST['time']);
		/*设置登录的会员对于last_time这个字段为now()*/
		$query="update user set last_time=now() where name='{$_POST['name']}'";
		execute($link,$query);
		skip('index.php','ok','登录成功！');exit();
	}else{
		skip('login.php', 'error', '用户名或密码填写错误！');exit();
	}
}

$template['title']='登录';
$template['css']=array('style/public.css','style/register.css');
?>
<?php include 'inc/header_inc.php';?>
<!--登录-->
<div id="register" >
	<h2>欢迎用户登录</h2>
	<form method="post" action=" " onsubmit="return check()">
		<label>用户名：<input type="text" id="name" name="name"/><span>*用户名含有禁用字符，请选择其他用户名</span></label>
		<label>密码：<input type="password" id="pw" name="pw" /><span>*用户名含有禁用字符，请选择其他用户名</span></label>
		<label>验证码：<input name="vcode" type="text" id="vcode"/><span>*请输入下方验证码</span></label>
		<img class="vcode" src="show_code.php" />
		<label>自动登录：
			<select style="width:236px;height:25px;" name="time">
				<option value="3600">1小时内</option>
				<option value="86400">1天内</option>
				<option value="259200">3天内</option>
				<option value="2592000">30天内</option>
			</select>
			<span>*公共电脑上请勿长期自动登录</span>
		</label>
		<div style="clear:both;"></div>
		<input class="btn" type="submit" name="submit" value="登录" />
	</form>
</div>
<script type="text/javascript">
	function check(){
		var username=document.getElementById('name').value;
		var password=document.getElementById('pw').value;
		var yanzhengma=document.getElementById('vcode').value;

		if(username===""){
			alert("请输入用户名!");
			return false;
		}
		
		if(password===""){
			alert("请输入密码!");
			return false;
		}
		
		if(yanzhengma===""){
			alert("请输入验证码!");
			return false;
		}
	}
</script>
<?php include 'inc/footer_inc.php';?>